//
//  QDWeatherView.m
//  QuickDoctor
//
//  Created by 李伟 on 2016/12/20.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDWeatherView.h"
#import "NetworkData.h"
#import "QDChangeProvinceVC.h"
#import "AppDelegate.h"
#import <UIImageView+WebCache.h>

@interface QDWeatherView()
@property (weak, nonatomic) IBOutlet UIImageView *img_icon;

@property (weak, nonatomic) IBOutlet UILabel *lb_weather;

@property (weak, nonatomic) IBOutlet UIButton *lb_city;

@property (weak, nonatomic) IBOutlet UILabel *lb_temperature;
@property (weak, nonatomic) IBOutlet UILabel *lb_date;

@end
@implementation QDWeatherView
#pragma mark
#pragma mark - 创建视图
+ (instancetype)weatherView
{
    UINib *nib = [UINib nibWithNibName:@"QDWeatherView" bundle:nil];
    
    return [[nib instantiateWithOwner:nil options:nil] lastObject];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    //接收通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeCity:) name:@"city" object:nil];

    [self getData:@"上海"];

}
#pragma mark
#pragma mark - 获取数据
- (void)getData:(NSString *)obj
{
    
    NetworkData *netWork = [NetworkData sharedNetworkData];
    [netWork getWeatherInfoWithCity:obj completeBlock:^(id responseObject, NSError *error) {
        
        _lb_date.text = [NSString stringWithFormat:@"%@",[responseObject[@"results"][0][@"weather_data"][0][@"date"]substringToIndex:9]];
        
        _lb_weather.text = responseObject[@"results"][0][@"weather_data"][0][@"weather"];
        
        _lb_temperature.text = responseObject[@"results"][0][@"weather_data"][0][@"temperature"];
        
        //获取系统时间
        NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
        [dateformate setDateFormat:@"HH"];
        NSString *nowTime = [dateformate stringFromDate:[NSDate date]];
        if ([nowTime intValue] > 18 || [nowTime intValue] <= 6) {
            [self.img_icon sd_setImageWithURL:[NSURL URLWithString:responseObject[@"results"][0][@"weather_data"][0][@"nightPictureUrl"]]];
        }
        else
        {
            [self.img_icon sd_setImageWithURL:[NSURL URLWithString:responseObject[@"results"][0][@"weather_data"][0][@"dayPictureUrl"]]];
        }
    }];
    
}
 #pragma mark - 点击改变城市
- (IBAction)clickChangeCity:(UIButton*)city
{
    QDChangeProvinceVC *provinceVC = [[QDChangeProvinceVC alloc]init];
    provinceVC.title = @"选择当前省份位置";
    
    [[self getCurrentViewC].navigationController pushViewController:provinceVC animated:YES];
}

#pragma mark
#pragma mark - 通知方法
- (void)changeCity:(NSNotification*)notif
{
    //这个方法不能设置Title,要通过set方法来设置;
//    _lb_city.titleLabel.text = notif.object;
    [_lb_city setTitle:notif.object forState:UIControlStateNormal];

    [self getData:notif.object];
}


#pragma mark
#pragma mark - 移除通知
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark
#pragma mark - 获取view的当前控制器
- (UIViewController*)getCurrentViewC
{
    for (UIView * next = [self superview];next; next = next.superview) {
        
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

-(NSString *)description
{
    return [self yy_modelDescription];
}


@end
